Skip to content

fix(android): parse large integers, exponents and escaped strings in CapacitorHttp JSON responses - #8708

Open
Swiezu wants to merge 2 commits into
ionic-team:mainfrom
Swiezu:fix/android-http-large-integer-response
Open

Swiezu wants to merge 2 commits into
ionic-team:mainfrom
Swiezu:fix/android-http-large-integer-response

Conversation

@Swiezu

@Swiezu Swiezu commented Nov 10, 2026

Copy link
Copy Markdown

Description

When a CapacitorHttp response is application/json and its whole body is a single JSON value (not an object or array), Android parses it by hand in HttpRequestHandler.parseJSON(). Three valid JSON values are handled differently than on iOS (JSONSerialization with .fragmentsAllowed) and on the web (fetch().json()):

Body iOS / web Android before Android after
8000010570 8000010570 request fails with NumberFormatException 8000010570
12345678901234567890 12345678901234567000 request fails with NumberFormatException 12345678901234567000
1e5 100000 the string "1e5" 100000
"a\"b" a"b a\"b (escapes kept) a"b

The first two cases are the worst: integers are parsed with Integer.parseInt, which throws for anything outside the 32-bit range, and the NumberFormatException is not caught by the surrounding catch (JSONException e). The app never gets the response. This is the bug reported in #7400 (closed for lack of a reproduction; the stack trace there points to the same line).

Reproduction

Any endpoint that returns a bare large integer with Content-Type: application/json, for example an ID or a timestamp in milliseconds:

// server responds with: 8000010570
const { data } = await CapacitorHttp.get({ url });

iOS and web resolve with 8000010570, Android rejects with For input string: "8000010570".

Change

parseJSON() keeps its structure and only the three value branches change:

  • Integers are parsed as Integer, then Long, then Double, so values that fit an int keep their current type and nothing else changes for them.
  • The decimal branch also accepts an exponent (1e5, 1.5E+3, -2.5e-3), as RFC 8259 allows.
  • A number outside the Double range (e.g. 1e400) is returned as the raw string, because JSObject rejects infinite values and the request would fail again.
  • A quoted string is unescaped with JSONTokener when the body is exactly one valid JSON string. Otherwise the previous behavior (content between the quotes) is kept.

Unit tests cover each case and readData() end to end. On current main, 5 of the new tests fail (3 of them with the NumberFormatException from #7400); with this change all 57 Android unit tests pass.

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation

Notes

  • Compared with Use JSONTokener for application/json reponse parsing #8087, which also fixes the large integer case by handing every value to JSONTokener.nextValue(): Android's readLiteral() reads integers with a leading 0 as octal (0123 becomes 83), stops unquoted text at the first space (an application/json error page Internal Server Error becomes "Internal") and ignores trailing content (12 34 becomes 12). This PR keeps the current results for those inputs and only changes the cases that fail or differ from iOS and web today.
  • Values beyond 2^53 lose precision once they reach JavaScript, the same as on iOS and web.

Related

#7400, #8087

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant